package org.mozhu.mboot.backend.security;

import lombok.extern.slf4j.Slf4j;
import org.mozhu.mboot.backend.system.account.entity.User;
import org.mozhu.mboot.backend.system.account.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

@Slf4j
public class CustomUserDetailsService implements UserDetailsService {

    @Autowired
    UserService userService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = userService.findByLoginName(username);
        if (user == null) {
            throw new UsernameNotFoundException("User of username[" + username + "] not found");
        }
        return user.userDetails();
    }

}
